
#include <iostream>
#include "cJSON.h"
#include <ctime>

int main(){
    //模拟令牌
    cJSON* cjson_token = NULL;
    cjson_token= cJSON_CreateObject();
    cJSON_AddNumberToObject(cjson_token, "coke", 2);
    cJSON_AddStringToObject(cjson_token, "name", "name");
    cJSON_AddStringToObject(cjson_token, "password", "password");
    char* token = cJSON_Print(cjson_token);
    //原始数据
    cJSON* cjson_data = NULL;
    cjson_data = cJSON_CreateObject();
    cJSON_AddNumberToObject(cjson_data,"windSpeed",0.0);
    cJSON_AddNumberToObject(cjson_data, "windScale", 0.0);
    cJSON_AddNumberToObject(cjson_data, "windDirection", 45);
    cJSON_AddNumberToObject(cjson_data, "ambientTemperature", 26.14);
    cJSON_AddNumberToObject(cjson_data, "ambientHumidity", 47.56);
    cJSON_AddNumberToObject(cjson_data, "pressure", 100.90);
    cJSON_AddNumberToObject(cjson_data, "rainfall", 0.0);
    cJSON_AddNumberToObject(cjson_data, "PM2.5", 31);
    cJSON_AddNumberToObject(cjson_data, "PM10", 40);
    cJSON_AddNumberToObject(cjson_data, "lightIntensity", 396);
//    std::cout << "原始数据：" << std::endl;
    char* oj = NULL;
    oj = cJSON_Print(cjson_data);
//    std::cout << oj << std::endl;
    //开始处理
    //第一步，解析
    cJSON *cjson_=NULL;
    cjson_=cJSON_CreateObject();
    cJSON* cjson_temperature = NULL;
    cJSON* cjson_humidity = NULL;
    cJSON* cjson_speed = NULL;
    cJSON* cjson_direction = NULL;
    cJSON* cjson_rain;
    cJSON* cjson_sunlight = NULL;
    cJSON* cjson_pm25 = NULL;
    cJSON* cjson_pm10 = NULL;
    cjson_ = cJSON_Parse(oj);
    cjson_temperature = cJSON_GetObjectItem(cjson_, "ambientTemperature");
    cjson_humidity = cJSON_GetObjectItem(cjson_, "ambientHumidity");
    cjson_speed = cJSON_GetObjectItem(cjson_, "windSpeed");
    cjson_direction = cJSON_GetObjectItem(cjson_, "windDirection");
    cjson_rain = cJSON_GetObjectItem(cjson_, "rainfall");
    cjson_sunlight = cJSON_GetObjectItem(cjson_, "lightIntensity");
    cjson_pm25 = cJSON_GetObjectItem(cjson_, "PM2.5");
    cjson_pm10 = cJSON_GetObjectItem(cjson_, "PM10");
    //第二步，封装
    cJSON* cjson_new = NULL;
    cjson_new=cJSON_CreateObject();
    cJSON_AddNumberToObject(cjson_new, "code", 5);
    cJSON_AddStringToObject(cjson_new, "token", token);
    std::cout<<"token:"<<token<<std::endl;
    cJSON_AddNumberToObject(cjson_new, "station", 9090);
    int time_;
    time_t t;
    time_ = time(&t);
    cJSON_AddNumberToObject(cjson_new, "time", time_);
    cJSON_AddNumberToObject(cjson_new, "temperature", cjson_temperature->valuedouble);
    cJSON_AddNumberToObject(cjson_new, "humidity", cjson_humidity->valuedouble);
    cJSON_AddNumberToObject(cjson_new, "speed", cjson_speed->valuedouble);
    cJSON_AddNumberToObject(cjson_new, "direction", cjson_direction->valuedouble);
    cJSON_AddNumberToObject(cjson_new, "rain", cjson_rain->valuedouble);
    cJSON_AddNumberToObject(cjson_new, "sunlight", cjson_sunlight->valuedouble);
    cJSON_AddNumberToObject(cjson_new, "pm25", cjson_pm10->valuedouble);
    cJSON_AddNumberToObject(cjson_new, "pm10", cjson_pm25->valuedouble);
    char *buf = NULL;
//    std::cout<<"直接用cjson_Print()输出"<<std::endl;
//    std::cout<<cJSON_Print(cjson_new)<<std::endl;
    buf=cJSON_Print(cjson_new);
    std::cout << "处理后的数据" << std::endl;
    std::cout << buf << std::endl;
    cJSON_Delete(cjson_data);
    cJSON_Delete(cjson_new);
    return 0;
}

